﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DLV.Data.Entities;
using DLV.FrameWork;

namespace DLV.BackEnd.Pages
{
    public partial class EditType : AdminPageBase
    {
        private TypeInfo _typeInfo = new TypeInfo();
        private readonly TypeController _typeController = new TypeController();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!String.IsNullOrEmpty(Request.QueryString["TypeId"]))
                _typeInfo = _typeController.GetType(int.Parse(Request.QueryString["TypeId"]));

            if (Page.IsPostBack) return;

            LoadStatus();
            LoadUser();
            LoadTypeOf();

            if (_typeInfo.TypeId == -1) return;

            RenderForm();
        }

        protected void lbtSave_Click(object sender, EventArgs e)
        {
            SaveData();
        }
        protected void lbtClose_Click(object sender, EventArgs e)
        {
            Response.Redirect("/Pages/ListType.aspx?xml=Type");
        }

        private void SaveData()
        {
            _typeInfo.TypeName = txtTypeName.Text;
            _typeInfo.TypeOf = int.Parse(ddlTypeOf.SelectedValue);
            _typeInfo.Status = Convert.ToInt32(ddlStatus.SelectedValue);

            if (_typeInfo.TypeId == -1)
            {
                _typeInfo.CreatedBy = ddlCreatedBy.SelectedValue == "-1"
                    ? UserId
                    : int.Parse(ddlCreatedBy.SelectedValue);
                _typeInfo.CreatedDate = DateTime.Now;
                _typeController.InsertType(_typeInfo);// chổ này nè
            }
            else
            {
                _typeInfo.UpdatedBy = UserId;
                _typeInfo.UpdatedDate = DateTime.Now;
                _typeController.UpdateType(_typeInfo);
            }

            Response.Redirect("/Pages/ListType.aspx?xml=Type");
        }

        private void RenderForm()
        {
            txtTypeName.Text = _typeInfo.TypeName;
            ddlStatus.SelectedValue = _typeInfo.Status.ToString();
            ddlTypeOf.SelectedValue = _typeInfo.TypeOf.ToString();
            ddlCreatedBy.SelectedValue = _typeInfo.CreatedBy.ToString();
        }
        private void LoadStatus()
        {
            var ds = new DataSet();
            ds.ReadXml(MapPath("~/xml/Status.xml"));
            var dv = ds.Tables["Status"].DefaultView;
            ddlStatus.DataTextField = "Name";
            ddlStatus.DataValueField = "ID";
            ddlStatus.DataSource = dv;
            ddlStatus.DataBind();
        }
         private void LoadTypeOf()
        {
            var ds = new DataSet();
            ds.ReadXml(MapPath("~/xml/ItemType.xml"));
            var dv = ds.Tables["ItemType"].DefaultView;
            ddlTypeOf.DataTextField = "Name";
            ddlTypeOf.DataValueField = "ID";
            ddlTypeOf.DataSource = dv;
            ddlTypeOf.DataBind();
        }
        private void LoadUser()
        {
            ddlCreatedBy.DataSource = new AdminUserController().ListAdminUser();
            ddlCreatedBy.DataTextField = "UserName";
            ddlCreatedBy.DataValueField = "UserId";
            ddlCreatedBy.DataBind();

            ddlCreatedBy.Items.Insert(0, new ListItem("--- Chọn người tạo ---", "-1"));
        }
    }
}